package com.mi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.ApplicationEntity;
import com.mi.entity.RecruitEntity;
import com.mi.service.RecruitService;
import com.mi.util.LoginAnno;
import com.mi.util.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

//人才招聘
@Controller
public class RecruitController {

    @Reference(check = false)
    RecruitService recruitService;

    //招聘职位
    @RequestMapping("recruitment")
    public String recruitment(RecruitEntity re, HttpServletRequest request){

        //调用service查询所有的招聘信息
        List<RecruitEntity> recruitEntities = recruitService.queryRecruits(re);

        request.setAttribute("recruitEntities",recruitEntities);

        return "recruitment";
    }

    //应聘职位
    @LoginAnno
    @RequestMapping("application")
    public String application(int r_id,HttpServletRequest request){

        //根据id查询招聘信息  将招聘职位名称返回到应聘表中进行展示
        RecruitEntity recruitEntity = recruitService.queryRecruitByid(r_id);

        request.setAttribute("recruitEntity",recruitEntity);

        return "application";
    }

    //提交应聘信息
    @RequestMapping("addApp")
    public String addApp(ApplicationEntity ae, MultipartFile resume,HttpServletRequest request){

        //上传简历
        if(resume.getOriginalFilename() != null && resume.getOriginalFilename()!= "") {
            //获取文件名,使用UUID避免文件名重复
            String fileName = UUIDUtil.getUUID()+".doc";

            String realPath = File.separator+"root"+File.separator+"static"+File.separator+"resume";

            File file = new File(realPath);

            //如果路径不存在就创建路径
            if (!file.exists()) {

                file.mkdirs();

            }

            //文件存放路径
            String path = realPath + File.separator + fileName;

            try (FileOutputStream fos = new FileOutputStream(path);     //上传简历
                 InputStream is = resume.getInputStream()) {

                FileCopyUtils.copy(is, fos);

            } catch (Exception e) {

                e.printStackTrace();

            }

            ae.setA_resume("/resume/" + fileName);
        }

        boolean b = recruitService.application(ae);

        return "success";

    }

    //培养与发展
    @RequestMapping("development")
    public String development(){
        return "development";
    }

    //薪资与报酬
    @RequestMapping("salary")
    public String salary(){
        return "salary";
    }

}
